﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;
using XCore.Redis;

namespace SaaS.Model.Dto.Users
{
    /// <summary>
    /// 用户信息传输对象
    /// </summary>
    public struct UserModel
    {
        /// <summary>
        /// 会员ID
        /// </summary>
        public int Id;
        /// <summary>
        /// 用户Id
        /// </summary>
        public string UserName;
        /// <summary>
        /// 租户关联Id数组 用户可创建多个租户
        /// </summary>
        public int[] Tenants;
        /// <summary>
        /// 注册时间
        /// </summary>
        public DateTime CreationTime;

        public static implicit operator HashEntry[](UserModel user)
        {
            return new[]
            {
                new HashEntry("Id",user.Id.ToRedisValue()),
                new HashEntry("UserName",user.UserName.ToRedisValue()),
                new HashEntry("CreationTime",user.CreationTime.ToRedisValue()),
                new HashEntry("Tenants",user.Tenants.ToRedisValue())
            };
        }

        public static implicit operator UserModel(HashEntry[] hash)
        {
            UserModel user = new UserModel();
            foreach (HashEntry item in hash)
            {
                switch (item.Name)
                {
                    case "Id":
                        user.Id = item.Value.GetRedisValue<int>();
                        break;
                    case "UserName":
                        user.UserName = item.Value.GetRedisValue<string>();
                        break;
                    case "CreationTime":
                        user.CreationTime = item.Value.GetRedisValue<DateTime>();
                        break;
                    case "Tenants":
                        user.Tenants = item.Value.GetRedisValue<int[]>();
                        break;
                    default:
                        break;
                }
            }
            return user;

        }

        public static implicit operator bool(UserModel user)
        {
            return user.Id == 0 ? false : true;
        }

    }
}
